package com.ruoyi.algorithm.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beihu.sightplus.common.request.PageRequest;
import org.apache.commons.lang.StringUtils;

/**
 * @author dyk
 * @date 2021/7/16 14:17
 * @description
 */
public class MybatisUtils {
    public static void getOrderBy(QueryWrapper queryWrapper, PageRequest pageRequest) {
        String orderAttr = pageRequest.getOrderAttr();
        String orderType = pageRequest.getOrderType();
        String field = ReflectionUtil.getTableFieldValue(queryWrapper.getEntityClass(), orderAttr);
        if (StringUtils.isEmpty(field)) {
            queryWrapper.orderByDesc("create_time");
        } else {
            if ("Asc".equalsIgnoreCase(orderType)) {
                queryWrapper.orderByAsc(field);
            } else {
                queryWrapper.orderByDesc(field);
            }
        }
    }
}
